package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.dto.SetmealDishDto;
import com.itheima.dto.SetmealDishDto2;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealVo;
import com.itheima.vo.SetmealVo2;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YangJiaHao
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public CommonResult page(String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<SetmealVo> pageInfo = setmealService.page(name,page,pageSize);
        return CommonResult.success(pageInfo);
    }

    @PostMapping
    public CommonResult addSetmeal(@RequestBody SetmealDishDto setmealDishDto){
        Boolean result = setmealService.addSetmeal(setmealDishDto);
        if (!result){
            return CommonResult.error("新增失败");
        }
        return CommonResult.success("新增成功");
    }

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable String id ){
        SetmealVo2 setmealVo = setmealService.getById(id);
        return CommonResult.success(setmealVo);
    }

    @PutMapping
    public CommonResult modifySetmeal(@RequestBody SetmealDishDto2 setmealDishDto2){
        setmealService.modifySetmeal(setmealDishDto2);
        return CommonResult.success("修改套餐信息成功");
    }

    @DeleteMapping
    public CommonResult delete(String ids){
        setmealService.delete(ids);
        return CommonResult.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public CommonResult status(@PathVariable Integer status, String ids){
        setmealService.status(status,ids);
        return CommonResult.success("操作成功");
    }
}
